package com.zenchn.utils;

import com.zenchn.enums.DataValueStyleEnum;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象 操作 工具类
 * @author dingxinfa
 * @date    2018-08-19
 */
public class ObjectUtils {


    /**
     * 队列比较
     * @param <T>
     * @param a
     * @param b
     * @return
     */
    public static <T extends Comparable<T>> boolean compareList(List<T> a, List<T> b) {
        if(a.size() != b.size()) {
            return false;
        }
        Collections.sort(a);
        Collections.sort(b);
        for(int i=0;i<a.size();i++){
            if(!a.get(i).equals(b.get(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     *     利用Introspector和PropertyDescriptor 将Bean --> Map
     */
    public static Map<String, Object> transAllBeanToMap(Object obj) {

        if(obj == null){
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!"class".equals(key)) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    map.put(key, value);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    public static Map<String, Object> transNotNullBeanToMap(Object obj) {

        if(obj == null){
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!"class".equals(key)) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if (null != value) {
                        map.put(key, value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /***
     * String 转 其他对象类型
     * @param valueStyle
     * @param value
     * @return
     */
    public static<T extends Comparable> T stringToObject(String valueStyle, String value){
        try {
            Class clazz= DataValueStyleEnum.getValueClass(valueStyle);

            if (Boolean.class.isAssignableFrom(clazz)) {
                return (T) Boolean.valueOf(Boolean.parseBoolean(value));
            } else if (Byte.class.isAssignableFrom(clazz)) {
                return (T) Byte.valueOf(Byte.parseByte(value));
            } else if (String.class.isAssignableFrom(clazz)) {
                return (T) value;
            } else if (Short.class.isAssignableFrom(clazz)) {
                return (T) Short.valueOf(Short.parseShort(value));
            } else if (Integer.class.isAssignableFrom(clazz)) {
                return (T) Integer.valueOf(Integer.parseInt(value));
            } else if (Float.class.isAssignableFrom(clazz)) {
                return (T) Float.valueOf(Float.parseFloat(value));
            } else if (Long.class.isAssignableFrom(clazz)) {
                return (T) Long.valueOf(Long.parseLong(value));
            } else if (Double.class.isAssignableFrom(clazz)) {
                return (T) Double.valueOf(Double.parseDouble(value));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return (T) value;
        }
        return (T) value;
    }
}
